package com.baomidou.springwind.controller.supply;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.service.NetworkSupplyService;
import com.baomidou.springwind.mapper.NetworkSupplyDao;
import com.baomidou.springwind.mapper.NetworkDepotDao;
import com.baomidou.springwind.mapper.NetworkDepotGoodsDao;
import com.baomidou.springwind.mapper.NetworkReliabilityDao;
import com.baomidou.springwind.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 物流网络规划可靠性
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/supply")
public class ReliabilityController extends BaseController {

    @Autowired
    private NetworkSupplyService networkSupplyService;

    @Autowired
    private NetworkSupplyDao networkSupplyDao;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private NetworkReliabilityDao networkReliabilityDao;

    @Autowired
    private NetworkDepotDao networkDepotDao;

    @Autowired
    private NetworkDepotGoodsDao networkDepotGoodsDao;
    /**
     * 客户分析主界面
     */
    @RequestMapping("/reliability")
    @Permission("1001")
    public String customGroup(Model model){
        EntityWrapper<NetworkDepot> wrapper = new EntityWrapper<NetworkDepot>();
        wrapper.eq("batch_id",getProjectId());
        wrapper.eq("depot_level", 1);
        model.addAttribute("prjList", networkDepotDao.selectList(wrapper));
        return pjaxPath("/lpp/network/supply/reliability/main", model);
    }

    //返回第一个测试数据
    @ResponseBody
    @Permission("1001")
    @RequestMapping("/reliability/loadData")
    public String laod1(){
        String selected=request.getParameter("selected");
        String selecteds[]=selected.split(",");
        JSONObject ret = build(selecteds);
        return callbackSuccess(ret);
    }
    //返回第一个测试数据
    @ResponseBody
    @Permission("1001")
    @RequestMapping("/reliability/test2")
    public String test2(){
        EntityWrapper<NetworkReliability> wrapper = new EntityWrapper<NetworkReliability>();
        wrapper.eq("batch_id",getProjectId());
        List<NetworkReliability> networkReliability=networkReliabilityDao.selectList(wrapper);
        JSONObject ret = JSON.parseObject(networkReliability.get(0).getBar2());
        return callbackSuccess(ret);
    }


    public JSONObject build(String selecteds[]){
        JSONObject rets = new JSONObject();
        JSONArray arr1=new JSONArray();
        JSONArray arr2=new JSONArray();
        JSONArray arr21=new JSONArray();
        JSONArray arr22=new JSONArray();
        JSONArray arr23=new JSONArray();
        JSONArray arr3=new JSONArray();
        JSONArray arr4=new JSONArray();
        JSONArray arr5=new JSONArray();
        JSONArray arr6=new JSONArray();
        JSONArray arr7=new JSONArray();
        JSONArray arr8=new JSONArray();
        EntityWrapper<NetworkDepot> wrapper0 = new EntityWrapper<NetworkDepot>();
        wrapper0.eq("depot_level", 1);
        wrapper0.eq("batch_id",getProjectId());
        List<NetworkDepot> mapList0 = networkDepotDao.selectList(wrapper0);
        for(int i=0;i<mapList0.size();i++){
            arr1.add(mapList0.get(i).getDepotName());
            arr2.add(mapList0.get(i).getDepotCapacity());
            arr21.add(mapList0.get(i).getDepotRent());
            arr22.add(mapList0.get(i).getWorkerCost());
            arr23.add(mapList0.get(i).getDeviceCost());
        }
        rets.put("arr1",arr1);
        rets.put("arr2",arr2);
        rets.put("arr21",arr21);
        rets.put("arr22",arr22);
        rets.put("arr23",arr23);
        rets.put("arr3",arr3);
        rets.put("arr4",arr4);
        rets.put("arr5",arr5);
        rets.put("arr6",arr6);
        rets.put("arr7",arr7);
        rets.put("arr8",arr8);
        return rets;
    }
}
